package cn.tedu.project.oa.attendance.service;

import cn.tedu.project.oa.attendance.mapper.AttendanceMapper;
import cn.tedu.project.oa.attendance.pojo.dto.AttendanceQuery;
import cn.tedu.project.oa.attendance.pojo.entity.Attendance;
import cn.tedu.project.oa.attendance.pojo.vo.AttendanceVO;
import cn.tedu.project.oa.dingding.DingDingService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.dingtalk.api.response.OapiAttendanceListResponse;
import com.dingtalk.api.response.OapiV2UserGetResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class AttendanceServiceImpl implements AttendanceService{
    @Autowired
    private AttendanceMapper attendanceMapper;
    @Autowired
    private DingDingService dingDingService;

    /**
     * 查询员工考勤
     *
     * @param id 员工考勤主键
     * @return 员工考勤
     */
    @Override
    public Attendance selectAttendanceById(Long id)
    {
        return attendanceMapper.selectById(id);
    }

    /**
     * 查询员工考勤列表
     *
     *
     * @return 员工考勤
     */
    @Override
    public List<AttendanceVO> selectAttendanceList(AttendanceQuery attendanceQuery) {
        log.debug("查询员工考勤列表入参:{}",attendanceQuery);
        QueryWrapper<Attendance> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(attendanceQuery.getUsername())){
            queryWrapper.like("username", attendanceQuery.getUsername());
        }
        if (attendanceQuery.getAttendanceDate() != null){
            queryWrapper.eq("attendance_date", attendanceQuery.getAttendanceDate());
        }
        if (StringUtils.isNotBlank(attendanceQuery.getCheckStatus())){
            queryWrapper.eq("check_status", attendanceQuery.getCheckStatus());
        }
        List<Attendance> attendanceList = attendanceMapper.selectList(queryWrapper);
        log.debug("查询员工考勤列表，查询结果：{}", attendanceList);
        List<AttendanceVO> attendanceVOList = new ArrayList<>();
        for (Attendance attendance : attendanceList){
            AttendanceVO attendanceVO = new AttendanceVO();
            BeanUtils.copyProperties(attendance, attendanceVO);
            attendanceVOList.add(attendanceVO);
        }
        log.debug("查询员工考勤列表，转换后结果：{}", attendanceVOList);
        return attendanceVOList;
    }

    /**
     * 新增员工考勤
     *
     * @return 结果
     */
    @Override
    public int insertAttendance(OapiAttendanceListResponse response) {
        int size = response.getRecordresult().size();
        response.getRecordresult().sort(Comparator.comparing(OapiAttendanceListResponse.Recordresult::getUserCheckTime));
        OapiAttendanceListResponse.Recordresult recordresult = response.getRecordresult().get(size-1);
        //获取钉钉用户信息用来绑定后台用户信息
        OapiV2UserGetResponse rsp = dingDingService.getUser(recordresult.getUserId());
        OapiV2UserGetResponse.UserGetResponse userGetResponse = rsp.getResult();

        log.debug("钉钉考勤回调参数recordresult：{}", response.getBody());
        log.debug("钉钉获取用户回调参数userGetResponse:{}", rsp.getBody());

        Attendance attendance = new Attendance();
        attendance.setUsername(userGetResponse.getName());
        attendance.setMobile(userGetResponse.getMobile());
        attendance.setAttendanceDate(recordresult.getWorkDate());
        attendance.setCheckType(recordresult.getCheckType());
        attendance.setCheckTime(recordresult.getUserCheckTime());
        attendance.setCheckStatus(recordresult.getTimeResult());
        attendance.setCreateTime(new Date());
        //判断是否有重复数据
        QueryWrapper<Attendance> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", attendance.getUsername());
        queryWrapper.eq("attendance_date", attendance.getAttendanceDate());
        queryWrapper.eq("check_time", attendance.getCheckTime());
        Long l = attendanceMapper.selectCount(queryWrapper);
        if (l > 0){
            log.debug("已有数据");
            return 0;
        }
        return attendanceMapper.insert(attendance);
    }


    /**
     * 修改员工考勤
     *
     *
     * @return 结果
     */



    /**
     * 查询今日打卡总数
     * @return
     */
    @Override
    public int count() {
        log.debug("查询今日打卡总数业务");

        LocalDate date = LocalDate.now();

        QueryWrapper<Attendance> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("attendance_date",date);
        Long l = attendanceMapper.selectCount(queryWrapper);

        int num = Integer.parseInt(String.valueOf(l));

        return num;
    }


}
